<?php
class ConexionBD {
    //private $servidor = "190.27.248.70";
    private $servidor = "127.0.0.1";
    private $puerto = "5432";
    private $baseDatos = "SuperCentroLosEjecutivos";
    private $usuario = "postgres";
    //private $contrasena = "123456";
    private $contrasena = "";
    private static $conexion;
    private $link;
    private $isOpen;

    private function __construct() {}

    public static function get() {
        if (!isset(self::$conexion)) self::$conexion = new ConexionBD();
        return self::$conexion;
    }

    public function conectarse() {
        if (!($this->link = pg_connect("host=" . $this->servidor . " port=" . $this->puerto . " dbname=" . $this->baseDatos . " user=" . $this->usuario . " password=" . $this->contrasena))) {
            echo json_encode(array("ERROR" => "Error conectando a la base de datos."));
            exit();
        }
        $this->isOpen = true;
        return $this->link;
    }

    public function desconectarse() { 
        if ($this->isOpen) {
            pg_close($this->link);
            $this->isOpen = false;
        }
    }
}
?>
